package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/admin/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO dto) {
        log.info("接单：{}", dto);
        Orders order = Orders.builder().id(dto.getId()).status(Orders.CONFIRMED).build();
        ordersService.updateStatus(order);
        return Result.success();
    }

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO dto) {
        log.info("拒单：{}", dto);
        Orders order = Orders.builder().id(dto.getId()).status(Orders.CANCELLED).rejectionReason(dto.getRejectionReason()).build();
        ordersService.updateStatus(order);
        return Result.success();
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO dto) {
        log.info("取消订单：{}", dto);
        Orders order = Orders.builder().id(dto.getId()).status(Orders.CANCELLED).cancelReason(dto.getCancelReason()).build();
        ordersService.updateStatus(order);
        return Result.success();
    }

    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        Orders order = Orders.builder().id(id).status(Orders.DELIVERY_IN_PROGRESS).build();
        ordersService.updateStatus(order);
        return Result.success();
    }

    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        Orders order = Orders.builder().id(id).status(Orders.COMPLETED).deliveryTime(LocalDateTime.now()).build();
        ordersService.updateStatus(order);
        return Result.success();
    }
}
